package com.softtown.modules.organization.controller;

import com.softtown.common.annotation.LogOperation;
import com.softtown.common.constant.Constant;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.page.PageData;
import com.softtown.common.regularUtils.RegularUtils;
import com.softtown.common.utils.Result;
import com.softtown.common.validator.ValidatorUtils;
import com.softtown.common.validator.group.DefaultGroup;
import com.softtown.modules.organization.dto.CompOrgPosnDTO;
import com.softtown.modules.organization.entity.CompOrgEmplPosnEntity;
import com.softtown.modules.organization.entity.CompOrgPosnEntity;
import com.softtown.modules.organization.service.CompOrgEmplPosnService;
import com.softtown.modules.organization.service.CompOrgPosnService;
import com.softtown.modules.organization.vo.CompOrgPosnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0
 * @ProjectName: softtown-aioffice
 * @ClassName: CompOrgPosnController
 * @Description: 公司岗位Controller层
 * @Author: fuming
 * @Date: 2019/6/5 10:59
 */
@RestController
@RequestMapping("/company/org")
@Api(tags = "公司岗位")
public class CompOrgPosnController {

    @Resource
    private CompOrgPosnService compOrgPosnService;

    @Resource
    private CompOrgEmplPosnService compOrgEmplPosnService;

    /**
     * @param @param [companyId]
     * @return @return com.softtown.common.utils.Result<java.util.List<com.softtown.modules.organization.vo.CompOrgPosnVO>>
     * @throws
     * @Title: getCompOrgDeptList
     * @Description: 根据所属公司的ID获取该公司下的所有岗位
     * @author fuming
     * @date 2019/6/14 11:13
     */
    @PostMapping("getCompOrgPosn")
    @ApiOperation("获取公司下的岗位信息")
    @LogOperation("获取公司下的岗位信息")
    public Result<List<CompOrgPosnVO>> getCompOrgDeptList(@RequestBody String companyId) {
        //前台传过来是JSON格式，所以要转换
        String compid = JSONObject.fromObject(companyId).get("companyId").toString();
        List<CompOrgPosnVO> posnListByCompId = compOrgPosnService.getPosnListByCompId(Long.valueOf(compid));
        return new Result<List<CompOrgPosnVO>>().ok(posnListByCompId);
    }

    @PostMapping("getCompExecPosn")
    @ApiOperation("获取公司下的执行人岗位信息")
    @LogOperation("获取公司下的执行人岗位信息")
    public Result<List<CompOrgPosnVO>> getCompExecPosn(@RequestBody String companyId) {
        //前台传过来是JSON格式，所以要转换
        String compid = JSONObject.fromObject(companyId).get("companyId").toString();
        List<CompOrgPosnVO> posnListByCompId = compOrgPosnService.getCompExecPosn(Long.valueOf(compid));
        return new Result<List<CompOrgPosnVO>>().ok(posnListByCompId);
    }

    /**
     * @param @param [compOrgPosnDTO]
     * @return @return com.softtown.common.utils.Result
     * @throws
     * @Title: addPosns
     * @Description: 岗位添加
     * @author fuming
     * @date 2019/6/9 14:03
     */
    @PostMapping("addPosn")
    @ApiOperation("添加岗位")
    @LogOperation("添加岗位")
    public Result addPosns(@RequestBody CompOrgPosnDTO compOrgPosnDTO) {
        Result result = new Result<>();
        //校验数据是否为空
        ValidatorUtils.validateEntity(compOrgPosnDTO, DefaultGroup.class);
        if (!RegularUtils.isNum(compOrgPosnDTO.getSort())) {
            return result.error(ErrorCode.INCORRECT_SORT_FORMAT);
        }
        List<CompOrgPosnVO> posnListByCompId = compOrgPosnService.getPosnListByCompId(compOrgPosnDTO.getCompId());
        for (CompOrgPosnVO orgPosn:posnListByCompId) {
            String posnName = orgPosn.getName();
            if (compOrgPosnDTO.getName().equals(posnName)) {
                return result.error(ErrorCode.POSITION_DATA_EXISTS);
            }
        }
        compOrgPosnService.addPosition(compOrgPosnDTO);
        return result;
    }

    /**
     * @param @param [compOrgPosnDTO]
     * @return @return com.softtown.common.utils.Result
     * @throws
     * @Title: updatePosnInfo
     * @Description: 岗位信息修改
     * @author fuming
     * @date 2019/6/9 14:20
     */
    @PostMapping("updatePosnInfos")
    @ApiOperation("修改岗位信息")
    @LogOperation("修改岗位信息")
    public Result updatePosnInfo(@RequestBody CompOrgPosnDTO compOrgPosnDTO) {
        Result result = new Result<>();
        String posnName = "";
        //校验数据是否为空
        ValidatorUtils.validateEntity(compOrgPosnDTO, DefaultGroup.class);
        if (!RegularUtils.isNum(String.valueOf(compOrgPosnDTO.getSort()))) {
            return result.error(ErrorCode.INCORRECT_SORT_FORMAT);
        }
        List<CompOrgPosnVO> posnListByCompId = compOrgPosnService.getPosnListByCompId(compOrgPosnDTO.getCompId());
        CompOrgPosnVO positionInfoById = compOrgPosnService.getPositionInfoById(compOrgPosnDTO.getId());
        for (CompOrgPosnVO orgPosn:posnListByCompId) {
            posnName = orgPosn.getName();
            if (compOrgPosnDTO.getName().equals(posnName) && !compOrgPosnDTO.getName().equals(positionInfoById.getName())) {
                return result.error(ErrorCode.POSITION_DATA_EXISTS);
            }
        }
        compOrgPosnService.updatePosnInfos(compOrgPosnDTO);
        return result;
    }

    /**
     * @param @param [id]
     * @return @return com.softtown.common.utils.Result<com.softtown.modules.organization.vo.CompOrgPosnVO>
     * @throws
     * @Title: getPosnInfo
     * @Description: 通过id获取岗位信息
     * @author fuming
     * @date 2019/6/15 14:16
     */
    @PostMapping("getPosnInfo")
    @ApiOperation("通过id获得岗位信息")
    @LogOperation("通过id获得岗位信息")
    public Result<CompOrgPosnVO> getPosnInfo(@RequestBody CompOrgPosnDTO compOrgPosnDTO) {
        CompOrgPosnVO posnInfoById = compOrgPosnService.getPosnInfoById(compOrgPosnDTO.getId());
        return new Result<CompOrgPosnVO>().ok(posnInfoById);
    }

    /**
     * 分页
     *
     * @param params
     * @return com.softtown.common.utils.Result<com.softtown.common.page.PageData < com.softtown.modules.organization.vo.CompOrgPosnVO>>
     * @Author fuming
     * @Date 2020/4/13 20:38
     */
    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "compId", value = "公司id", paramType = "query", dataType = "String")
    })
    public Result<PageData<CompOrgPosnVO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<CompOrgPosnVO> page = compOrgPosnService.page(params);
        return new Result<PageData<CompOrgPosnVO>>().ok(page);
    }

    /**
     * 岗位删除
     *
     * @param id 岗位id
     * @return com.softtown.common.utils.Result
     * @Author fuming
     * @Date 2020/4/13 20:39
     */
    @PostMapping("deletePosn")
    @ApiOperation("岗位删除")
    @LogOperation("岗位删除")
    public Result deletePosition(@RequestBody String id) {
        Result result = new Result<>();
        String posnId = JSONObject.fromObject(id).get("id").toString();
        CompOrgPosnVO positionInfoById = compOrgPosnService.getPositionInfoById(Long.valueOf(posnId));
        if (positionInfoById != null && positionInfoById.getEmplId() != null) {
            return result.error(ErrorCode.DATA_EXISTS);
        } else {
            compOrgPosnService.deletePosition(Long.valueOf(posnId));
            return result;
        }
    }

    /**
     * 岗位激活或禁用
     *
     * @param id 岗位id
     * @return com.softtown.common.utils.Result
     * @Author fuming
     * @Date 2020/4/13 20:40
     */
    @PostMapping("PosnActivate")
    @ApiOperation("岗位激活或禁用")
    @LogOperation("岗位激活或禁用")
    public Result activate(@RequestBody String id) {
        Result result = new Result<>();
        String posnId = JSONObject.fromObject(id).get("id").toString();
        CompOrgPosnVO positionInfoById = compOrgPosnService.getPositionInfoById(Long.valueOf(posnId));
        if (positionInfoById.getEmplId() != null) {
            return result.error(ErrorCode.POSITION_FORBIDDEN_DATA_USED);
        }
        compOrgPosnService.activeOrgPosn(Long.valueOf(posnId));
        return result;
    }

    /**
     * 查找用户岗位
     *
     * @param valueId id值
     * @return com.softtown.common.utils.Result<java.util.List < java.lang.Long>>
     * @Author Dingyayong
     * @Date 2020/4/13 20:40
     */
    @PostMapping("findEmplUserPosn")
    @ApiOperation("查找用户岗位")
    @LogOperation("查找用户岗位")
    public Result<List<Long>> findEmplUserPosn(@RequestBody String valueId) {
        String emplId = JSONObject.fromObject(valueId).get("emplId").toString();
        String compId = JSONObject.fromObject(valueId).get("compId").toString();
        List<CompOrgEmplPosnEntity> compOrgEmplPosnEntities = compOrgEmplPosnService.findListPosnEntity(Long.valueOf(compId), Long.valueOf(emplId));
        List<Long> posnIdList = new ArrayList<>();
        for (CompOrgEmplPosnEntity compOrgEmplPosnEntity : compOrgEmplPosnEntities) {
            posnIdList.add(compOrgEmplPosnEntity.getPosnId());
        }
        return new Result<List<Long>>().ok(posnIdList);
    }

    /**
     * 根据公司id找出对应的岗位
     *
     * @param compOrgPosnDTO
     * @return List<CompOrgPosnEntity>
     * @author Dingyayong
     * @date 2019/9/25 10:46
     */
    @PostMapping("getFindPosnByCompId")
    @ApiOperation("根据公司id找出对应的岗位")
    @LogOperation("根据公司id找出对应的岗位")
    public Result<List<CompOrgPosnEntity>> getFindPosnByCompId(@RequestBody CompOrgPosnDTO compOrgPosnDTO) {
        List<CompOrgPosnEntity> compOrgPosnEntities =
                compOrgPosnService.getFindPosnByCompId(compOrgPosnDTO.getCompId());
        return new Result<List<CompOrgPosnEntity>>().ok(compOrgPosnEntities);
    }

    /**
     * 查找岗位
     *
     * @param valueId id值
     * @return com.softtown.common.utils.Result<java.util.List < java.lang.String>>
     * @Author fuming
     * @Date 2020/4/13 20:41
     */
    @PostMapping("findEmplPosn")
    @ApiOperation("查找岗位")
    @LogOperation("查找岗位")
    public Result<List<String>> findEmplPosn(@RequestBody String valueId) {
        String emplId = JSONObject.fromObject(valueId).get("emplId").toString();
        String compId = JSONObject.fromObject(valueId).get("compId").toString();
        List<String> posnNameByEmplIdAndCompId = compOrgEmplPosnService.getPosnNameByEmplIdAndCompId(Long.valueOf(emplId), Long.valueOf(compId));
        return new Result<List<String>>().ok(posnNameByEmplIdAndCompId);
    }
}
